//
//  CNMapViewController.m
//  BeeQuick_19
//
//  Created by tanky on 2016/9/26.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNMapViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
#import <BaiduMapAPI_Radar/BMKRadarComponent.h>

@interface CNMapViewController ()<BMKMapViewDelegate,BMKGeoCodeSearchDelegate>

@property(nonatomic,weak)BMKMapView* mapView;
@property(nonatomic,strong)BMKLocationService *locService;
@property(nonatomic,strong)BMKGeoCodeSearch *geocodesearch;


@end

@implementation CNMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = mapView;
    _mapView = mapView;
    _mapView.mapType = BMKMapTypeStandard;
    
    //初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    //启动LocationService
    [_locService startUserLocationService];
    
    _geocodesearch = [[BMKGeoCodeSearch alloc] init];
    //编码服务的初始化(就是获取经纬度,或者获取地理位置服务)
    _geocodesearch.delegate = self;//设置代理为self
    
    _geocodesearch = [[BMKPoiSearch alloc] init];
    _geocodesearch.delegate = self;
}

- (void)viewWillAppear:(BOOL)animated{
    [_mapView viewWillAppear];
    _mapView.delegate = self;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [_mapView viewWillDisappear];
    _mapView.delegate = nil;
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidAppear:(BOOL)animated{
    // 添加一个PointAnnotation
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor;
    coor.latitude = 39.915;
    coor.longitude = 116.404;
    annotation.coordinate = coor;
    annotation.title = @"您的收货地址";
    [_mapView addAnnotation:annotation];
}

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
        return newAnnotationView;
    }
    return nil;
}

//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    NSLog(@"位置信息: 纬度: %f,经度: %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
}

- (void)geocoderAction
{
    BMKGeoCodeSearch *geocoderSearcher =[[BMKGeoCodeSearch alloc]init];
    geocoderSearcher.delegate = self;
    BMKGeoCodeSearchOption *geoCodeSearchOption = [[BMKGeoCodeSearchOption alloc]init];
    geoCodeSearchOption.city= @"上海市";
    geoCodeSearchOption.address = @"田子坊";
    BOOL flag = [geocoderSearcher geoCode:geoCodeSearchOption];
    if (flag)
    {
        NSLog(@"geo检索发送成功");
    }
    else
    {
        NSLog(@"geo检索发送失败");
    }
    
}

#pragma mark - BMKGeoCodeSearchDelegate
- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    if (error == BMK_SEARCH_NO_ERROR) {
        //在此处理正常结果
        
        // 创建大头针
        BMKPointAnnotation *anno = [[BMKPointAnnotation alloc] init];
        anno.coordinate = CLLocationCoordinate2DMake(result.location.latitude, result.location.longitude);
        anno.title = result.address;
        
        // 移除之前的大头针
        [_mapView removeAnnotations:_mapView.annotations];
        // 添加到地图上
        [_mapView addAnnotation:anno];
        
        NSLog(@"地址:%@",result.address);
        
    }
    else {
        NSLog(@"抱歉，未找到结果");
    }
}


@end
